package com.smx.service;

import com.smx.dao.BookDao;
import com.smx.model.entity.BorrowBooks;
import com.smx.model.vo.BooksVo;
import com.smx.model.vo.BorrowBooksVo;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @ClassName BooksService
 * @Description TODO
 * @Author moses
 * @Date 2021/5/10
 **/
public class BooksService {


    public static List<BooksVo> getAlByPage(int begin,int end) {
        List<BooksVo> books =BookDao.getAllByPage(begin,end);
        List<Integer> bookIds = BookDao.getAllStoreBookIdByUserId(1);
        List<BorrowBooksVo> borrowBooks = BookDao.getAllBorrowBooksByUserId(1);

        books.forEach(b->{
            if (bookIds.contains(b.getId())) {
                b.setStore(1);
            } else {
                b.setStore(0);
            }

            b.setBorrow(0);
            borrowBooks.forEach(x ->{
                if (x.getBookId() == b.getId()) {
                    b.setBorrow(1);
                }
            });
        });
        return books;
    }

    public static List<BooksVo> getAllWithStoreStatusByUserId(int userId) {
        List<BooksVo> books = BookDao.getAll();
        List<Integer> bookIds = BookDao.getAllStoreBookIdByUserId(userId);

        books.forEach(b->{
            if (bookIds.contains(b.getId())) {
                b.setStore(1);
            } else {
                b.setStore(0);
            }
        });
        return books;
    }

    public static int getAllCount() {
        return BookDao.getAllCount();
    }

    public static int addStoreId(int bookId,int userId) {
        return BookDao.addStoreId(bookId, userId);
    }

    public static int removeStore(int bookId, int userId) {
        return BookDao.removeStore(bookId, userId);
    }

    public static List<BooksVo> getStoreBookByUserId(int userId,int pageNum,int pageSize) {
        List<BooksVo> books = BookDao.getAll();
        List<Integer> bookIds = BookDao.getAllStoreBookIdByUserId(userId);
        List<BooksVo> bookss = new ArrayList<>();
        books.forEach(b->{
            if (bookIds.contains(b.getId())) {
                b.setStore(1);
                bookss.add(b);
            }
        });
        if (pageNum != 0 && pageSize != 0) {
            Stream<BooksVo> pageBooks = bookss.stream().skip((pageNum - 1) * pageSize).limit(pageSize);
            return pageBooks.collect(Collectors.toList());
        } else {
            return bookss;
        }
    }

    public static List<BorrowBooksVo> getBorrowBooksByUserId(int userId, int begin, int end) {
        return BookDao.getBorrowBooksByUserId(userId,begin,end);
    }

    public static int getBorrowBooksCountByUserId(int userId) {
        return BookDao.getBorrowBooksCountByUserId(userId);
    }

    public static int insertBorrowBook(BorrowBooks b) {
        return BookDao.insertBorrowBook(b);
    }

    public static List<BorrowBooksVo> getHistoryBorrowBooksByUserIdPage(int userId, int begin, int end) {
        return BookDao.getHistoryBorrowBooksByUserIdPage(userId, begin, end);
    }

    public static int getHistoryBorrowBooksCountByUserId(int userId) {
        return BookDao.getHistoryBorrowBooksCountByUserId(userId);
    }

    public static int removeBorrowBook(int bookId, int userId) {
        return BookDao.removeBorrowBook(bookId, userId);
    }

    public static int updateBorrowBook(String illegal,int bookId, int userId) {
        return BookDao.updateBorrowBook(illegal,bookId, userId);
    }


}
